<template>
    <el-dialog :title="dialogTitle" :visible.sync="isVisible">
        <el-form ref="timeDiscountForm" :model="timeDiscountForm" :rules="rules" label-width="100px">

            <el-form-item label="名称" prop="orderTimeName">
                <el-input v-model="timeDiscountForm.orderTimeName" />
            </el-form-item>
            <el-form-item label="时长" prop="day">
                <el-input v-model="timeDiscountForm.day" />
            </el-form-item>
            <el-form-item label="折扣" prop="orderTimeDiscount">
                <el-input v-model="timeDiscountForm.orderTimeDiscount" />
            </el-form-item>
            <el-form-item label="状态" prop="state">
                <el-radio-group v-model="timeDiscountForm.state.code">
                    <el-radio label="ENABLE">启用</el-radio>
                    <el-radio label="DISABLE">禁用</el-radio>
                </el-radio-group>
            </el-form-item>
            <el-form-item label="排序" prop="sortValue">
                <el-input v-model="timeDiscountForm.sortValue" />
            </el-form-item>

            <el-form-item>
                <el-button type="primary" plain size="small" @click="submitForm('timeDiscountForm')">保存</el-button>
            </el-form-item>
        </el-form>
    </el-dialog>
</template>
<script>
import timeDiscountApi from '@/api/init/timeDiscount'
export default {
    name: 'Edit',
    props: {
        dialogVisible: {
            type: Boolean,
            default: false
        },
        type: {
            type: String,
            default: 'add'
        },
        title: {
            type: String,
            default: '添加'
        }
    },
    data() {
        return {
            // 因为不能直接修改 props 接收到的值，所以用一个变量来接收，修改这个变量
            dialogType: this.type,
            dialogTitle: this.title,
            timeDiscountForm: {
                orderTimeName: '',
                day: 0,
                orderTimeDiscount: 0.0,
                sortValue: 0,
                state: {code: 'ENABLE', desc: '启用'}
            },
            rules: {
                orderTimeName: [
                    { required: true, message: '请输入名称', trigger: 'blur' },
                    { min: 2, max: 20, message: '长度在 2 到 20 个字符', trigger: 'blur' }
                ]
            }
        }
    },
    computed: {
        isVisible: {
            get() {
                return this.dialogVisible
            },
            set() {
                this.close()
            }
        }
    },
    methods: {
        close() {
            this.$emit('close')
        },
        submitForm(formName) {
            let _this = this
            this.$refs[formName].validate(valid => {
                if (valid) {
                    // 判断是添加还是修改
                    if (_this.dialogType == 'add') {
                        timeDiscountApi.add(this.timeDiscountForm).then(() => {
                            _this.isVisible = false
                            _this.$emit('success')
                        })
                    } else {
                        timeDiscountApi.update(this.timeDiscountForm).then(() => {
                            _this.isVisible = false
                            _this.$emit('success')
                        })
                    }
                } else {
                    return false
                }
            })
        },
        getTimeDiscount(id) {
            // 清空表单
            this.resetForm('timeDiscountForm')
            // 回显数据
            timeDiscountApi.get(id).then(res => {
                this.timeDiscountForm = res.data.data
            })
        },
        resetForm(formName) {
            this.$refs[formName].resetFields()
        }
    }
}
</script>
